﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace TodayWhoModel
{
    public class UserPreferencesModel
    {
        #region fields
        private int pid;

        public int Pid
        {
            get { return pid; }
            set { pid = value; }
        }
        private int userId;

        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }
        private string bgColor;

        public string BgColor
        {
            get { return bgColor; }
            set { bgColor = value; }
        }
        private string bgType;

        public string BgType
        {
            get { return bgType; }
            set { bgType = value; }
        }
        private string boxColor;

        public string BoxColor
        {
            get { return boxColor; }
            set { boxColor = value; }
        }
        private float opacity;

        public float Opacity
        {
            get { return opacity; }
            set { opacity = value; }
        }
        private string nameColor;

        public string NameColor
        {
            get { return nameColor; }
            set { nameColor = value; }
        }
        private string titleColor;

        public string TitleColor
        {
            get { return titleColor; }
            set { titleColor = value; }
        }
        private string shortDescColor;

        public string ShortDescColor
        {
            get { return shortDescColor; }
            set { shortDescColor = value; }
        }
        private string nameFont;

        public string NameFont
        {
            get { return nameFont; }
            set { nameFont = value; }
        }
        private string titleFont;

        public string TitleFont
        {
            get { return titleFont; }
            set { titleFont = value; }
        }
        private string shortDescFont;

        public string ShortDescFont
        {
            get { return shortDescFont; }
            set { shortDescFont = value; }
        }
        private string nameFontSize;

        public string NameFontSize
        {
            get { return nameFontSize; }
            set { nameFontSize = value; }
        }
        private string titleFontSize;

        public string TitleFontSize
        {
            get { return titleFontSize; }
            set { titleFontSize = value; }
        }
        private string shortDescFontSize;

        public string ShortDescFontSize
        {
            get { return shortDescFontSize; }
            set { shortDescFontSize = value; }
        }
        private int boxWidth;

        public int BoxWidth
        {
            get { return boxWidth; }
            set { boxWidth = value; }
        }
        private int boxHeight;

        public int BoxHeight
        {
            get { return boxHeight; }
            set { boxHeight = value; }
        }
        private string boxTop;

        public string BoxTop
        {
            get { return boxTop; }
            set { boxTop = value; }
        }
        private string boxLeft;

        public string BoxLeft
        {
            get { return boxLeft; }
            set { boxLeft = value; }
        }
        #endregion

        #region database methods

        public bool Insert()
        {
            string ssql = @"INSERT INTO [User-Preferences] (userId,bgColor,bgType,boxColor,opacity,nameColor,titleColor,shortDescColor,nameFont,titleFont,
                                                                shortDescFont,nameFontSize,titleFontSize,shortDescFontSize,boxWidth,boxHeight,boxTop,boxLeft) 
                                     VALUES(@userId,@bgColor,@bgType,@boxColor,@opacity,@nameColor,@titleColor,@shortDescColor,@nameFont,@titleFont,@shortDescFont,@nameFontSize,
                                                @titleFontSize,@shortDescFontSize,@boxWidth,@boxHeight,@boxTop,@boxLeft);";

            SqlCommand cmd = new SqlCommand(ssql);
            cmd.Parameters.AddWithValue("@userId", UserId);
            cmd.Parameters.AddWithValue("@bgColor", "#1a2330");
            cmd.Parameters.AddWithValue("@bgType", "Center");
            cmd.Parameters.AddWithValue("@boxColor", "#000000");
            cmd.Parameters.AddWithValue("@opacity", 0.5);
            cmd.Parameters.AddWithValue("@nameColor", "#00ff00");
            cmd.Parameters.AddWithValue("@titleColor", "#005fbf");
            cmd.Parameters.AddWithValue("@shortDescColor", "#ffff00");
            cmd.Parameters.AddWithValue("@nameFont", "Courier New");
            cmd.Parameters.AddWithValue("@titleFont", "Georgia");
            cmd.Parameters.AddWithValue("@shortDescFont", "Comic Sans MS");
            cmd.Parameters.AddWithValue("@nameFontSize", "45");
            cmd.Parameters.AddWithValue("@titleFontSize", "30");
            cmd.Parameters.AddWithValue("@shortDescFontSize", "20");
            cmd.Parameters.AddWithValue("@boxWidth", "500");
            cmd.Parameters.AddWithValue("@boxHeight", "500");
            cmd.Parameters.AddWithValue("@boxTop", "0");
            cmd.Parameters.AddWithValue("@boxLeft", "0");

            return DatabaseHelper.ExecuteNonQuerySqlWithoutIdentity(cmd);
        }

        
        public void RetrieveUserPreferences()
        {
            string ssql = "SELECT * FROM [User-Preferences]  WHERE userId=@userId ";

            SqlCommand cmd = new SqlCommand(ssql);

            cmd.Parameters.AddWithValue("@userId", UserId);


            SqlDataReader reader = DatabaseHelper.ExecuteSelectSql(cmd);
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    this.UserId = Int32.Parse(reader["userId"].ToString());
                    this.Pid = Int32.Parse(reader["pid"].ToString());
                    this.BgColor = reader["bgColor"].ToString();
                    this.BgType = reader["bgType"].ToString();
                    this.BoxColor = reader["boxColor"].ToString();
                    this.Opacity = float.Parse(reader["opacity"].ToString());
                    this.NameColor = reader["nameColor"].ToString();
                    this.TitleColor = reader["titleColor"].ToString();
                    this.ShortDescColor = reader["shortDescColor"].ToString();
                    this.NameFont = reader["nameFont"].ToString();
                    this.TitleFont = reader["titleFont"].ToString();
                    this.ShortDescFont = reader["shortDescFont"].ToString();
                    this.NameFontSize = reader["nameFontSize"].ToString();
                    this.TitleFontSize = reader["titleFontSize"].ToString();
                    this.ShortDescFontSize = reader["shortDescFontSize"].ToString();
                    this.BoxWidth =  Int32.Parse(reader["boxWidth"].ToString());
                    this.BoxHeight =  Int32.Parse(reader["boxHeight"].ToString());
                    this.BoxTop = reader["boxTop"].ToString();
                    this.BoxLeft =  reader["boxLeft"].ToString();
                    
                }
            }
        }

        public bool UpdatePref(string key, string value)
        {
            string ssql = "UPDATE [User-Preferences] SET " + key + "=@value WHERE userId=@userId";

            SqlCommand cmd = new SqlCommand(ssql);

            cmd.Parameters.AddWithValue("@userId", UserId);
            cmd.Parameters.AddWithValue("@value", value);

            return DatabaseHelper.ExecuteNonQuerySqlWithoutIdentity(cmd);
        }

        #endregion
    }
}
